/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Dao;




import Util.ConSQL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author use
 */
public class HuyHP {
    DangkiHP dangkiHP = new DangkiHP();
    ConSQL conn = new ConSQL();
    public int laySoTC(String maMon) throws Exception{
        Statement st = conn.getCon().createStatement();
        ResultSet rs = st.executeQuery("select soTC from monHoc where maMH='"+maMon+"'");
        if(rs.next()){
            return Integer.parseInt(rs.getString(1));
        }
        return Integer.parseInt(rs.getString(1));
    }
    public void createTitle(Vector tableTitle){
        tableTitle.removeAllElements();
        tableTitle.add("Mã lóp học phần");
        tableTitle.add("Tên lớp học phần");
        tableTitle.add("Ngày bắt đầu");
        tableTitle.add("Ngày kết thúc");
        tableTitle.add("Mã Khoa");
        tableTitle.add("Mã niên khóa");
        tableTitle.add("Mã môn");
        tableTitle.add("Số tín chỉ");        
        tableTitle.add("Chỉ tiêu");        
        tableTitle.add("Số đăng kí");
        tableTitle.add("Giáo viên");
    }
    public void createRecord(Vector tableRecord,ResultSet rs) throws SQLException, Exception{
        tableRecord.removeAllElements();
        while(rs.next()){
            Vector data = new Vector();
            data.add(rs.getString(1));
            data.add(rs.getString(2));
            data.add(rs.getString(6));
            data.add(rs.getString(7));
            data.add(rs.getString(3));
            data.add(rs.getString(4));
            data.add(rs.getString(5));
            data.add(laySoTC(rs.getString(5)));
            data.add(rs.getString(8));
            data.add(rs.getString(9));
            data.add(dangkiHP.layTenGiaovienDay(rs.getString(1)));
            tableRecord.add(data);
        }
    }
    public JTable timLopHP(String maSV,JTable table,Vector tableRecord,Vector tableTitle) throws Exception{
        tableRecord.removeAllElements();
        tableTitle.removeAllElements();
        
        createTitle(tableTitle);
        
        Statement st = conn.getCon().createStatement();
        ResultSet rs = st.executeQuery("select lopHP.maLopHP,tenLopHP,makhoa,maNK,maMH,ngayBatdau,ngayKetthuc,siSo,soHSDK from lopHP,lopHP_SV where lopHP.maLopHP=lopHP_SV.maLopHP and lopHP_SV.maSV='"+maSV+"';");
        
        createRecord(tableRecord, rs);
        table.setModel(new DefaultTableModel(tableRecord, tableTitle));
        return table;
    }
    public void huyHP(String maSV,JTable table,Vector tableRecord) throws Exception{
        int iDongchon = table.getSelectedRow();
        if(iDongchon==-1){
            JOptionPane.showMessageDialog(table, "Bạn chưa chọn học phần cần hủy");
        }
        else{            
            Vector vDongchon = (Vector) tableRecord.get(iDongchon);
            String maHP = vDongchon.get(0).toString();
            if(JOptionPane.showConfirmDialog(table, "Bạn chắc chắn muốn hủy học phần có mã: "+maHP, "Lựa chọn", JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION){
                Statement st = conn.getCon().createStatement();            
                int rs = st.executeUpdate("delete from lopHP_SV where maSV='"+maSV+"' and maLopHP='"+maHP+"';");
                JOptionPane.showMessageDialog(table, "Hủy thành công");
            }
        }
    }
    public int TongTC(JTable table,Vector tableRecord){        
        int sodong = table.getRowCount();
        int tongTC=0;
        for (int i=0;i<sodong;i++){
                Vector v = (Vector) tableRecord.get(0);
                String soTC = v.get(7).toString();
                tongTC += Integer.parseInt(soTC);
        }
        return tongTC;
    }
}
